const api = window.$api.system.moduleex

const modules = {
    namespaced: true,
    state:{
        list:[],
        group:{},
        map:{},
        menu:[]
    },
    actions:{
        getList({ commit }){
            return new Promise(resolve => {
                api.getList().then(res => {
                    const data = res.data.data
                    let modules = window.$deepClone(data)
                    let modulesGroup = {}
                    let modulesMap = {}

                    modules.forEach(ele => {
                        modulesGroup[ele.f_ParentId] = modulesGroup[ele.f_ParentId] || []
                        modulesGroup[ele.f_ParentId].push(ele)
                        modulesMap[ele.f_ModuleId] = ele
                    })

                    commit('SET_MODULES_EX', modules)
                    commit('SET_MODULESGROUP_EX', modulesGroup)
                    commit('SET_MODULESMAP_EX', modulesMap)
                    resolve(modules)
                })
            })
        }
    },
    mutations:{
        SET_MODULES_EX: (state, modules) => {
            state.list = modules;
        },
        SET_MODULESGROUP_EX: (state, modulesGroup) => {
            state.group = modulesGroup;
        },
        SET_MODULESMAP_EX: (state, modulesMap) => {
            state.map = modulesMap;
        }
    }
}

export default modules;